<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="openDialog()"
          >添加分类</el-button
        >
      </div>
      <div>
        <el-table :data="categoryList" border style="width: 100%" stripe>
          <el-table-column type="index" prop="date" label="序号" width="100">
          </el-table-column>
          <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
          <el-table-column prop="cate_alias" label="分类别名">
          </el-table-column>
          <el-table-column prop="address" label="操作">
            <template v-slot="scope">
              <el-button
                type="primary"
                size="mini"
                @click="updateCategory(scope.row)"
                >修改</el-button
              >
              <el-button type="danger" size="mini" @click="deleteFn(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <!-- 添加文章分类---对话框 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="dialogVisible"
      width="600px"
      @closed="dialogClose"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="cateName">
          <el-input v-model="addForm.cateName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cateAlias">
          <el-input v-model="addForm.cateAlias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCategoryAPI, addCateAPI, updateCateAPI, deleteCateAPI } from '@/api'

export default {
  name: 'CateGory',
  data () {
    return {
      categoryList: [],
      addForm: {
        cateName: '',
        cateAlias: ''
      },
      isEdit: false, // true为编辑框，false为添加框
      editId: '', // 当前编辑的类别id
      addFormRules: {
        cateName: [
          { required: true, message: '请输入分类名称！', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cateAlias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      },
      // 添加文章分类对话框是否可见
      dialogVisible: false
    }
  },
  created () {
    // 获取文章分类列表
    this.getCategoryList()
  },
  methods: {
    // 获取文章分类列表
    async getCategoryList () {
      const res = await getCategoryAPI()
      // console.log(res)
      this.categoryList = res.data.data
    },
    // 打开对话框
    openDialog () {
      this.isEdit = false
      this.editId = ''
      this.dialogVisible = true
    },
    // 对话框内确定按钮
    confirmFn () {
      this.$refs.addFormRef.validate(async (valid) => {
        if (valid) {
          if (this.isEdit) {
            // 修改
            // this.addForm.id = this.editId
            // const { data: res } = await updateCateAPI(this.addForm)
            const { data: res } = await updateCateAPI({
              id: this.editId,
              ...this.addForm
            })
            if (res.code !== 0) return this.$message.error(res.message)
            // 提示添加成功
            this.$message.success(res.message)
          } else {
            // 新增
            const { data: res } = await addCateAPI(this.addForm)
            // console.log(res)
            if (res.code !== 0) return this.$message.error(res.message)
            // 提示添加成功
            this.$message.success(res.message)
          }
          // 重新加载文章分类
          this.getCategoryList()
          // 关闭对话框
          this.dialogVisible = false
        } else {
          return false
        }
      })
    },
    // 对话框关闭时回调
    dialogClose () {
      this.$refs.addFormRef.resetFields()
    },
    // 修改分类名
    updateCategory (row) {
      this.isEdit = true
      this.editId = row.id
      this.dialogVisible = true
      // console.log(row)
      this.$nextTick(() => {
        this.addForm.cateName = row.cate_name
        this.addForm.cateAlias = row.cate_alias
      })
    },
    // 删除分类->点击事件
    async deleteFn (row) {
      const { data: res } = await deleteCateAPI(row.id)
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.getCategoryList()
    }
  }
}
</script>

<style scoped lang="less">
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
